#!/bin/sh
#REM *****************************************************
#REM 启动脚本
#REM 脚本参数说明
#REM
#REM RUNABLE_JAR                -当前启动的jar包，目前只取第一个jar，支持自定义
#REM PROFILE                    -当前运行profile配置，牧人default
#REM LOCATION                   -当前yml配置文件的路径，默认./config/
#REM JAVA_OPTS                  -jvm参数配置，支持自定义
#REM SKY_AGENT                  -agent配置路径，支持自定义
#REM JMX_AGEMT                  -agent配置路径，支持自定义
#REM GC_OPTS                    -jvm的GC策略配置，支持自定义
#REM CPU                        -当前系统逻辑CPU个数，默认2c
#REM MEM                        -当前系统内存，单位GB，默认4G
#REM EXEC_CMDLINE               -程序启动命令
#REM *****************************************************

RUNABLE_JAR=""
PROFILE="U6"
LOCATION=""
JAVA_OPTS=""
SKY_AGENT=""
JMX_AGEMT=""
GC_OPTS=""
CPU=""
MEM=""

#非当前目录执行需cd到微服务所在目录
#cd /home/zyb/apps/msp-demo/testCA

if [ -z "$GC_OPTS" ];then
    GC_OPTS="-XX:SurvivorRatio=7 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemakrEnable -XX:+UseParNewGC -XX:ParalleCMSThread=$CPU -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=10 -XX:+ScavengeBeforeFullGC -XX:+CMSscavengeBeforeRemark -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses"
fi

if [ -z "$RUNABLE_JAR" ];
    then
    FILES=${LS ../*}
    for FILENAME IN $FILES
    do 
        if [ "${FILENAME##*.}"x = "jar"x ]
            then
            RUNABLE_JAR=$FILENAME
            break
    fi
        done
fi


if [ -z "$RUNABLE_JAR" ] || [! -f "$RUNABLE_JAR"];
    then 
    echo "jar 文件不存在!"
    exit 
fi

if [ -z "$JAVA_HOME" ]; then
    JAVA_CMD=`which java`
else
    JAVA_CMD="$JAVA_HOME/bin/java"
fi


if [ ! -x "$JAVA_CMD" ]; then
    echo "The JAVA_HMOE enviroment Variable is not defined correctly" >&2
    echo "This environment variable is needed to run this program" >&2
    echo "NB: JAVA_HOME should point to a JDK not a JRE" >&2
    exit 1
fi

if [ -z "$JAVA_OPTS"];
    then
        if [ $EME -le 4 ]; then JAVA_OPTS="-server -D64 -Xmx2g -Xms2g -Xmn1g -XX:MaxDirectMemorySize=256m"
        elif [ $EME -le 8 ]; then JAVA_OPTS="-server -D64 -Xmx6g -Xms6g -Xmn3g -XX:MaxDirectMemorySize=512m"
        elif [ $EME -le 16 ]; then JAVA_OPTS="-server -D64 -Xmx12g -Xms12g -Xmn6g -XX:MaxDirectMemorySize=1g"
        fi
fi


if [ -z "$SKY_AGENT" ] && [ -z "$JMX_AGEMT" ];
    then 
        SKY_AGENT=${ls ../../skywalking-agent*/skywalking-agent.jar}
        JMX_AGEMT=${ls ../../jmx-exporter*/jmx_prometheus_javaagent*.jar}
fi


if [ -z "$PROFILE"];
    then PROFILE="default"
fi

if [ -z "$LOCATION"];
    then LOCATION="../config/"
fi



if [ -f "$SKY_AGENT" ] && [ -f "$JMX_AGEMT" ];
    then JMX_FOLDER=$(dirname $(readlink -f "$JMX_AGEMT")) EXEC_CMDLINE="$JAVA_CMD $JAVA_OPS $GC_OPTS -javaagent:$SKY_AGENT -javaagent:$JMX_AGEMT=16666:$JMX_FOLDER/config.yml -jar $RUNABLE_JAR --spring.profiles.active=$PROFILE --spring.config.location=$LOCATION"
    else EXEC_CMDLINE="$JAVA_CMD $JAVA_OPTS $GC_OPTS -jar $RUNABLE_JAR --spring.profiles.active=$PROFILE --spring.config.location=$LOCATION"
fi


#echo nohup $EXEC_CMDLINE "&"
#nohup $EXEC_CMDLINE 2>&1 &
nohup EXEC_CMDLINE >/dev/null 2>&1






